package com.ymttest.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymatou.app.api.parameter.AddAddressBean;
import com.ymatou.app.api.parameter.AddressListBean;
import com.ymatou.app.api.parameter.AppLoginBean;
import com.ymatou.app.api.parameter.DeleteAddressBean;
import com.ymatou.app.api.parameter.GroupCreateBean;
import com.ymatou.app.api.parameter.GroupJoinBean;
import com.ymatou.app.api.parameter.TopicProductDetailsBean;
import com.ymatou.app.api.service.AddAddressCall;
import com.ymatou.app.api.service.AddressListCall;
import com.ymatou.app.api.service.AppLoginCall;
import com.ymatou.app.api.service.DeleteAddressCall;
import com.ymatou.app.api.service.GroupCreateCall;
import com.ymatou.app.api.service.GroupJoinCall;
import com.ymatou.app.api.service.TopicProductDetailsCall;
import com.ymt.core.tool.Logger;
import com.ymttest.utils.TestDataManager;

public class YmtCallService {

	/*
	 * protected String AccessToken; protected Integer UserId;
	 * 
	 * public YmtCallService(String UserName, String PassWord){ AppLoginBean
	 * apploginbean = new AppLoginBean(); AppLoginCall applogincall = new
	 * AppLoginCall(); apploginbean.setUserName(UserName);
	 * apploginbean.setPassword(PassWord); applogincall.setData(false,apploginbean);
	 * try { applogincall.callService(); } catch (ClientProtocolException e) {
	 * e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
	 * this.AccessToken = applogincall.getAccessToken(); this.UserId =
	 * applogincall.getUserId(); }
	 */
	/**
	 * 创建团
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> createGroup(String AccessToken,
			int topicid) {
		Map<String, Object> map = new HashMap<String, Object>();
		GroupCreateBean groupcreatebean = new GroupCreateBean();
		GroupCreateCall groupcreatecall = new GroupCreateCall();
		try {
			groupcreatebean.setAccessToken(AccessToken);// 登陆token
			groupcreatebean.setTopicId(topicid);// 主题ID
			groupcreatecall.setData(false, groupcreatebean);
			groupcreatecall.callService();
			// Logger.debug("dddd"+groupcreatecall.getReturnData());
			map.put("GroupCode", groupcreatecall.getGroupCode());
			map.put("TopicId", topicid);
		} catch (Exception e) {
			Logger.fail(e);
		}
		return map;
	}

	/**
	 * 输入用户名密码登陆
	 * 
	 * @param 用户名
	 *            、密码
	 * @return Map
	 */
	public static Map<String, Comparable> appLogin(String UserName,
			String PassWord) {
		Map<String, Comparable> map = new HashMap<String, Comparable>();
		AppLoginBean apploginbean = new AppLoginBean();
		AppLoginCall applogincall = new AppLoginCall();
		try {
			apploginbean.setUserName(UserName);
			apploginbean.setPassword(PassWord);
			applogincall.setData(false, apploginbean);
			applogincall.callService();
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("UserId", applogincall.getUserId());
		map.put("AccessToken", applogincall.getAccessToken());
		return map;
	}

	/**
	 * 输入用户名密码参加团
	 */
	public static void GroupJoin(String AccessToken, int TopicId,
			String GroupCode) {
		GroupJoinBean groupjoinbean = new GroupJoinBean();
		GroupJoinCall groupjoincall = new GroupJoinCall();
		try {
			groupjoinbean.setGroupCode(GroupCode);
			groupjoinbean.setTopicId(TopicId);
			groupjoinbean.setAccessToken(AccessToken);
			groupjoincall.setData(false, groupjoinbean);
			groupjoincall.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 根据主题Id查询团产品信息
	 * 
	 * @return
	 */
	public static Map<String, Comparable> getTopicProductDetails(
			String AccessToken, int TopicId) {
		Map map = new HashMap();
		TopicProductDetailsBean topicproductdetailsbean = new TopicProductDetailsBean();
		TopicProductDetailsCall topicproductdetailscall = new TopicProductDetailsCall();
		try {
			topicproductdetailsbean.setTopicId(TopicId);
			topicproductdetailsbean.setAccessToken(AccessToken);
			topicproductdetailscall.setData(false, topicproductdetailsbean);
			topicproductdetailscall.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
		map.put("CatalogId", topicproductdetailscall.getCatalogId());
		map.put("SellerId", topicproductdetailscall.getSellerId());
		// Logger.debug(topicproductdetailscall.getReturnData());
		return map;
	}

	/**
	 * 查询指定用户的地址Id
	 * 
	 * @param AccessToken
	 * @param UserId
	 * @return
	 */
	public static List getAddressId(String AccessToken, int UserId) {
		List list = new ArrayList();
		AddressListBean addresslistbean = new AddressListBean();
		AddressListCall addresslistcall = new AddressListCall();
		try {
			addresslistbean.setAccessToken(AccessToken);
			addresslistbean.setUserId(UserId);
			addresslistcall.setData(false, addresslistbean);
			Logger.comment("查询用户:" + UserId + "收货地址");
			addresslistcall.callService();
			Logger.debug("查询指定用户的地址id call："
					+ addresslistcall.getEntityString());
			Logger.debug("查询指定用户的地址id response："
					+ addresslistcall.getReturnData());
			list = addresslistcall.getAddressId();
		} catch (Exception e) {
			Logger.fail(e);
		}
		// Logger.debug(addresslistcall.getReturnData());
		return list;
	}

	/**
	 * 删除指定用户的指定地址信息
	 * 
	 * @param AccessToken
	 * @param UserId
	 * @param AddressId
	 */
	public static void delAddressById(String AccessToken, int UserId,
			Integer AddressId) {
		DeleteAddressBean deladdressbean = new DeleteAddressBean();
		DeleteAddressCall deladdresscall = new DeleteAddressCall();
		try {
			deladdressbean.setAddressId(AddressId);
			deladdressbean.setUserId(UserId);
			deladdressbean.setAccessToken(AccessToken);
			deladdresscall.setData(false, deladdressbean);
			deladdresscall.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 添加收货地址
	 */
	public static int addAddress(String AccessToken, int UserId) {
		AddAddressBean addaddressbean = new AddAddressBean();
		AddAddressCall addaddresscall = new AddAddressCall();
		int addressid = 0;
		try {
			addaddressbean.setUserId(UserId);
			String Address1 = "上海市上海市闸北区灵石路695号3号楼1404室";
			addaddressbean.setAddress1(Address1);
			String ProvinceName = "上海市";
			addaddressbean.setProvinceName(ProvinceName);
			String CityName = "上海市";
			addaddressbean.setCityName(CityName);
			String DistrictName = "闸北区";
			addaddressbean.setDistrictName(DistrictName);
			String Details = "灵石路695号3号楼1404室";
			addaddressbean.setDetails(Details);
			String PostCode = "200001";
			addaddressbean.setPostCode(PostCode);
			String MobileNum = TestDataManager.getPhoneNum();
			addaddressbean.setMobile(MobileNum);
			String Recipient = TestDataManager.getChineseName();
			addaddressbean.setRecipient(Recipient);
			String Email = TestDataManager.getEmail();
			addaddressbean.setEmail(Email);
			boolean IsDefault = false;
			addaddressbean.setIsDefault(IsDefault);
			addaddressbean.setAccessToken(AccessToken);
			// 设置数据，发送
			addaddresscall.setData(false, addaddressbean);
			Logger.comment("给用户添加:" + UserId + "添加一个收货地址");
			addaddresscall.callService();
			Logger.debug("添加用户地址 call：" + addaddresscall.getEntityString());
			Logger.debug("添加用户地址 response：" + addaddresscall.getReturnData());
			addressid = addaddresscall.getAddressId();
		} catch (Exception e) {
			Logger.fail(e);
		}
		return addressid;
	}

	/**
	 * 添加收货地址，返回addressId
	 * 
	 * @param AccessToken
	 * @param userId
	 * @param mobilePhone
	 * @return addressId
	 */
	public static int addAddress(String accessToken, int userId,
			String mobilePhone) {
		AddAddressBean addaddressbean = new AddAddressBean();
		AddAddressCall addaddresscall = new AddAddressCall();
		Integer addressId = null;
		try {
			addaddressbean.setUserId(userId);
			String address1 = "上海市上海市闸北区灵石路695号3号楼1404室";
			addaddressbean.setAddress1(address1);
			String provinceName = "上海市";
			addaddressbean.setProvinceName(provinceName);
			String cityName = "上海市";
			addaddressbean.setCityName(cityName);
			String districtName = "闸北区";
			addaddressbean.setDistrictName(districtName);
			String details = "灵石路695号3号楼1404室";
			addaddressbean.setDetails(details);
			String postCode = "200001";
			addaddressbean.setPostCode(postCode);
			// String mobileNum = TestDataManager.getPhoneNum();
			addaddressbean.setMobile(mobilePhone);
			String recipient = TestDataManager.getChineseName();
			addaddressbean.setRecipient(recipient);
			String email = TestDataManager.getEmail();
			addaddressbean.setEmail(email);
			boolean isDefault = false;
			addaddressbean.setIsDefault(isDefault);
			addaddressbean.setAccessToken(accessToken);
			// 设置数据，发送
			addaddresscall.setData(false, addaddressbean);
			addaddresscall.callService();
			Logger.debug("添加收货:" + addaddresscall.getReturnData());
			addressId = addaddresscall.getAddressId();
		} catch (Exception e) {
			Logger.fail(e);
		}
		return addressId;
	}

	/**
	 * 添加收货地址，返回addressId
	 * 
	 * @param AccessToken
	 * @param userId
	 * @param mobilePhone
	 * @param name
	 *            收货人
	 * @return addressId
	 */
	public static int addAddress(String accessToken, int userId,
			String mobilePhone, String name) {
		AddAddressBean addaddressbean = new AddAddressBean();
		AddAddressCall addaddresscall = new AddAddressCall();
		Integer addressId = null;
		try {
			addaddressbean.setUserId(userId);
			String address1 = "上海市上海市闸北区灵石路695号3号楼1404室";
			addaddressbean.setAddress1(address1);
			String provinceName = "上海市";
			addaddressbean.setProvinceName(provinceName);
			String cityName = "上海市";
			addaddressbean.setCityName(cityName);
			String districtName = "闸北区";
			addaddressbean.setDistrictName(districtName);
			String details = "灵石路695号3号楼1404室";
			addaddressbean.setDetails(details);
			String postCode = "200001";
			addaddressbean.setPostCode(postCode);
			addaddressbean.setMobile(mobilePhone);
			String recipient = name;
			addaddressbean.setRecipient(recipient);
			String email = TestDataManager.getEmail();
			addaddressbean.setEmail(email);
			boolean isDefault = false;
			addaddressbean.setIsDefault(isDefault);
			addaddressbean.setAccessToken(accessToken);
			// 设置数据，发送
			addaddresscall.setData(false, addaddressbean);
			addaddresscall.callService();
			Logger.debug("添加收货:" + addaddresscall.getReturnData());
			addressId = addaddresscall.getAddressId();
		} catch (Exception e) {
			Logger.fail(e);
		}
		return addressId;
	}

	public static void main(String[] args) {
		/*	new YmtCallService()
					.addAddress(
							"E76DCD5974EEF851F856627AFEB9860DEFC8BB929FBD84F6B6D431C4DB3A646F22FFD97A2E22C6186421BE24F4A8D7281A6BD5C0C6238BD7",
							20000278, "13812345678");*/
		/*		int UserId = 20565603;
				AppCallService app=new AppCallService();
			*/

	}
}
